<?php

namespace app\index\service;

use app\common\model\Trade;
use app\index\dao\TradeDao;
use app\index\model\UM as UM;

/**
 * Class TradeInfoService
 * @package app\index\service
 */
class TradeInfoService
{
    // dao
    private TradeDao $tradeDao;

    public function __construct(TradeDao $tradeDao)
    {
        $this->tradeDao = $tradeDao;
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return Trade
     */
    public function read(UM $user, int $id): ?Trade
    {
        $trade = $this->tradeDao->getTradeById($id);

        if (!$trade) {
            return null;
        }

        if ($trade->memberId != $user->id) {
            return null;
        }

        $server = null;

        if (is_id($trade->serverId)) {
            $server = $this->getLandingService()->getServer($trade->serverId, $trade->serverSkuId);
        }

        $trade->setRelation('server', $server);

        return $trade;
    }

    private function getLandingService(): LandingService
    {
        return app(LandingService::class);
    }

}
